package com.banksoft.admin.service.impl;

import com.banksoft.admin.bean.Status;
import com.banksoft.admin.dao.BaseMyBatisDao;
import com.banksoft.admin.dao.ProductDao;
import com.banksoft.admin.dao.ProductParamDao;
import com.banksoft.admin.entity.Product;
import com.banksoft.admin.entity.ProductParam;
import com.banksoft.admin.service.ProductService;
import com.banksoft.util.StringUtil;
import com.banksoft.util.UUIDFactory;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by vanjoger on 2016/12/31.
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductDao productDao;
    @Autowired
    BaseMyBatisDao baseMyBatisDao;

    @Override
    public JSONObject save(Product product, String goldIn, String goldOut, String generalIn, String generalOut, String partnerIn, String partnerOut, String generalGoldIn, String generalGoldOut, String memberIn) {

        if (StringUtil.isNotNull(product.getId())) {//编辑
            this.baseMyBatisDao.update(ProductDao.class, product);

            ProductParam params = this.baseMyBatisDao.findById(ProductParamDao.class,product.getId());
            boolean update = true;
            if(params==null){
                params = new ProductParam();
                params.setId(product.getId());
                update = false;
            }
            params.setMemberIn(memberIn);
            params.setGoldIn(goldIn);
            params.setGoldOut(goldOut);
            params.setGeneralIn(generalIn);
            params.setGeneralOut(generalOut);
            params.setPartnerIn(partnerIn);
            params.setPartnerOut(partnerOut);
            params.setGeneralGoldIn(generalGoldIn);
            params.setGeneralGoldOut(generalGoldOut);
            if(update){
                this.baseMyBatisDao.update(ProductParamDao.class,params);
            }else{
                this.baseMyBatisDao.insert(ProductParamDao.class,params);
            }

        } else {//新增
            product.setId(UUIDFactory.random());
            product.setCreateTime(new Date().getTime());
            product.setStatus(Status.NORMAL.name());
            this.baseMyBatisDao.insert(ProductDao.class, product);

            ProductParam  params = new ProductParam();

            params.setId(product.getId());
            params.setMemberIn(memberIn);
            params.setGoldIn(goldIn);
            params.setGoldOut(goldOut);
            params.setGeneralIn(generalIn);
            params.setGeneralOut(generalOut);
            params.setPartnerIn(partnerIn);
            params.setPartnerOut(partnerOut);
            params.setGeneralGoldIn(generalGoldIn);
            params.setGeneralGoldOut(generalGoldOut);
            this.baseMyBatisDao.insert(ProductParamDao.class,params);
        }
        return StringUtil.FmtResult(true,"保存成功",null);

    }
    @Override
    public List<Product> listProduct() {
        return this.productDao.listProduct();
    }

    @Override
    public List<Product> listByOrderId(String orderId) {
        return this.productDao.listByOrderId(orderId);
    }
}
